package br.com.werp.suporte.Intranet;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 12/09/2005 10:43:13
 * Last Modify Date 21/12/2005 13:38:10
 */

public class AniversariantesDetalheAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_update_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      update_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("back");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void update_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    AniversariantesDetalheActionForm f =
      (AniversariantesDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Aniversariantes table =
      new br.com.werp.suporte.dbobj.table.Aniversariantes();
    Vector sets = new Vector(10, 2);
    table.setAniv_id(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_id()));
    table.setAniv_nome(f.getAniv_nome());
    if (f.getAniv_nome() != null) {
      sets.addElement("aniv_nome");
    } else {
      sets.addElement("");
    };
    table.setAniv_dia(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_dia()));
    if (f.getAniv_dia() != null) {
      sets.addElement("aniv_dia");
    } else {
      sets.addElement("");
    };
    table.setAniv_mes(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_mes()));
    if (f.getAniv_mes() != null) {
      sets.addElement("aniv_mes");
    } else {
      sets.addElement("");
    };
    String[] set = new String[sets.size()];
    for (int i = 0; i < sets.size(); i++) {
      set[i] = (String) sets.elementAt(i);
    }
    int updated = j.update(table, set);
    if (updated == 0) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsupdated",": " + updated));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    } else {
      Vector res_Aniversariantes = (Vector) session.getAttribute("res_Aniversariantes");
      if (res_Aniversariantes != null) {
        res_Aniversariantes.setElementAt(table, 0);
        br.com.werp.suporte.dbobj.table.Aniversariantes r =
          (br.com.werp.suporte.dbobj.table.Aniversariantes)
          res_Aniversariantes.elementAt(0);
        f.setAniv_id(r.getAniv_id() + "");
        f.setAniv_nome((r.getAniv_nome() != null) ? r.getAniv_nome() + "":
            "");
        f.setAniv_dia(r.getAniv_dia() + "");
        f.setAniv_mes(r.getAniv_mes() + "");
      }
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_delete_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      delete_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("back");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void delete_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    AniversariantesDetalheActionForm f =
      (AniversariantesDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Aniversariantes table =
      new br.com.werp.suporte.dbobj.table.Aniversariantes();
    table.setAniv_id(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_id()));
    table.setAniv_nome(f.getAniv_nome());
    table.setAniv_dia(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_dia()));
    table.setAniv_mes(
      com.egen.util.text.FormatNumber.parseInt(f.getAniv_mes()));
    j.delete(table);
    if (false) {
      Vector res_Aniversariantes = (Vector) session.getAttribute("res_Aniversariantes");
      if (res_Aniversariantes != null) {
        res_Aniversariantes.removeElementAt(0);
        br.com.werp.suporte.dbobj.table.Aniversariantes r =
          (br.com.werp.suporte.dbobj.table.Aniversariantes)
          res_Aniversariantes.elementAt(0);
        f.setAniv_id(r.getAniv_id() + "");
        f.setAniv_nome((r.getAniv_nome() != null) ? r.getAniv_nome() + "":
            "");
        f.setAniv_dia(r.getAniv_dia() + "");
        f.setAniv_mes(r.getAniv_mes() + "");
      }
    } else {
      f.reset(mapping, request);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_resetfull_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.removeAttribute("res_Aniversariantes");
      form.reset(mapping, request);
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();
      select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              (e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          (e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    AniversariantesDetalheActionForm f =
      (AniversariantesDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Aniversariantes table =
      new br.com.werp.suporte.dbobj.table.Aniversariantes();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    Object[][] where = { {"Aniv_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getAniv_id())}, {"Aniv_nome",
            "like", f.getAniv_nome()}, {"Aniv_dia","=",
                com.egen.util.text.FormatNumber.toInt(f.getAniv_dia())}, {"Aniv_mes",
                    "=", com.egen.util.text.FormatNumber.toInt(f.getAniv_mes())} };
    Vector res_Aniversariantes =
      j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Aniversariantes",res_Aniversariantes);
    if (res_Aniversariantes != null && res_Aniversariantes.size() > 0) {
      br.com.werp.suporte.dbobj.table.Aniversariantes r =
        (br.com.werp.suporte.dbobj.table.Aniversariantes)
        res_Aniversariantes.elementAt(0);
      f.setAniv_id(r.getAniv_id() + "");
      f.setAniv_nome((r.getAniv_nome() != null) ? r.getAniv_nome() + "":"");
      f.setAniv_dia(r.getAniv_dia() + "");
      f.setAniv_mes(r.getAniv_mes() + "");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

}
